package com.msh.artascope.devcloud.service.service.impl;


import com.msh.artascope.devcloud.service.service.EnvService;
import com.msh.artascope.devcloud.service.dao.DefalutResourceDao;
import com.msh.artascope.devcloud.service.model.query.DefalutResourceQueryVo;
import com.msh.artascope.devcloud.service.model.vo.DefalutResourceVo;
import javax.annotation.Resource;

import com.msh.frame.client.base.TenantBaseServiceImpl;
import com.msh.frame.interfaces.IdGenerateable;
import com.msh.artascope.devcloud.client.po.DefalutResourcePo;
import com.msh.artascope.devcloud.client.qo.DefalutResourceQo;
import com.msh.artascope.devcloud.service.service.DefalutResourceService;
import com.msh.frame.client.common.Result;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import org.springframework.util.CollectionUtils;

/**
 * @author shihu
 * @mail m-sh@qq.com
 */
@Service
@Slf4j
public class DefalutResourceServiceImpl extends TenantBaseServiceImpl<DefalutResourcePo,DefalutResourceQo> implements DefalutResourceService {

  @Resource
  private EnvService envService;

  @Resource
  private DefalutResourceDao defalutResourceDao;

  @Resource
  private IdGenerateable idGenerateable;

  @Override
  public long insert(DefalutResourcePo param) {
    param.setId(idGenerateable.getUniqueId());
    return super.insert(param);
  }

  @Override
  public int insertCollection(Collection<DefalutResourcePo> param) {
    param.stream().forEach(p->p.setId(idGenerateable.getUniqueId()));
    return super.insertCollection(param);
  }


  @Override
  public DefalutResourcePo getByEnvId(Long envId, Long tenantId) {
    return listOne(new DefalutResourceQo().setEnvId(envId).setTenantId(tenantId));
  }


// ----------前端vo处理-------------------------------------------------------

  private DefalutResourceVo toVo(DefalutResourcePo po){
    DefalutResourceVo vo = po.convert(DefalutResourceVo.class);
    return vo;
  }

  private List<DefalutResourceVo> toVoList(List<DefalutResourcePo> list){
    if(CollectionUtils.isEmpty(list)){
      return Collections.emptyList();
    }
    return list.stream().map(po->{
      return toVo(po);
    }).collect(Collectors.toList());
  }

  @Override
  public DefalutResourceVo getVo(Long id, Long tenantId) {
    DefalutResourcePo po = this.get(id, tenantId);
    if(null == po){
      return null;
    }
    DefalutResourceVo vo = toVo(po);

    return vo;
  }

  @Override
  public List<DefalutResourceVo> listVo(DefalutResourceQueryVo q){
    DefalutResourceQo qo = q.convert(DefalutResourceQo.class);
    List<DefalutResourcePo> poList = this.list(qo);
    List<DefalutResourceVo> voList = toVoList(poList);

    return voList;
  }

  @Override
  public Result<List<DefalutResourceVo>> pageVo(DefalutResourceQueryVo param) {
    DefalutResourceQo qo = param.convert(DefalutResourceQo.class);
    int count = this.count(qo);
    if (0 == count) {
      return Result.successReturn(Collections.<DefalutResourceVo>emptyList()).setCount(count);
    }
    List<DefalutResourcePo> poList = this.list(qo);
    List<DefalutResourceVo> voList = toVoList(poList);

    return Result.successReturn(voList).setCount(count);
  }


}
